/**
 * Demo2场景：
 *  业务主键为name，需要在后台程序中判断name不能重复（或设置表的unique index)
 *  数据库中表的实际主键为id（bigint类型），虚拟主键
 *  这种场景在新增记录时，数据库会返回自增id
 *  ==> <a-table>中的key需要用id，
 *  ==> 新增记录后，要将后台返回的记录 添加到<a-table>中
 *
 * @type {*[]}
 */
const dataSource = [
    {
        id: 1,
        "name": "name1",
        "region": "shanghai",
        "date1": "2020-04-20 16:04:74",
        "delivery": true,
        "type": ["2", "1"],
        "resource": "1",
        "desc": "desc"
    },
    {
        id: 2,
        "name": "name2",
        "region": "shanghai",
        "date1": "2020-04-20 17:04:74",
        "delivery": false,
        "type": ["1"],
        "resource": "1",
        "desc": "aa"
    }
];
export default {
    'GET /api/delivery-2/list': {
        rtnCode: "0000000",
        rtnMesg: "success",
        oary: dataSource
    },
    /**
     * 新增
     * @param req
     * @param res
     * @returns {*|Promise<any>}
     */
    'POST /api/delivery-2/add': (req, res) => {
        const payload = req.body;

        if (dataSource.findIndex(d => d.name == payload.name) != -1) {
            return res.json({
                rtnCode: "3720002",
                rtnMesg: "重复的记录"
            });
        } else {
            //此处模拟数据库，返回自增主键id
            let nextId = dataSource.map(d => d.id).reduce((id1, id2) => {
                return id1 > id2 ? id1 : id2;
            });

            //将数据库新生成的记录返回给前端页面
            payload.id = nextId;
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success",
                oary: [payload]
            });
        }
    },
    /**
     * 修改记录
     * @param req
     * @param res
     * @returns {*|Promise<any>}
     */
    'PUT /api/delivery-2/update': (req, res) => {
        const payload = req.body;

        //根据主键id找到需要更新的记录
        let updateRowIndex = dataSource.findIndex(d => d.id == payload.id);

        //如果找不到报错
        if (updateRowIndex != -1) {
            return res.json({
                rtnCode: "3720003",
                rtnMesg: "找不到需要更新的记录"
            });
        } else {
            //模拟更新数据库对应记录
            dataSource[updateRowIndex] = payload;

            //数据库已成功更新，将结果返回给前端页面(其实结果和输入的是一样的,这里的返回只是为了符合restful标准)
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success",
                oary: [payload]
            });
        }
    },
    /**
     * 删除方法:可以只传id(推荐)，也可以传整个对象
     * @param req
     * @param res
     */
    'DELETE /api/delivery-2/update': (req, res) => {
        const payload = req.body;

        //根据主键id找到需要更新的记录
        let deleteRowIndex = dataSource.findIndex(d => d.id == payload.id);

        //如果找不到报错
        if (deleteRowIndex != -1) {
            return res.json({
                rtnCode: "3720004",
                rtnMesg: "找不到需要删除的记录"
            });
        } else {
            //模拟数据库删除记录
            dataSource.splice(deleteRowIndex, 1);
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success"
            });
        }
    }
}